//
//  WBVisitorView.swift
//  个人微博
//
//  Created by 任聪聪 on 2017/6/19.
//  Copyright © 2017年 任孜聪. All rights reserved.
//

import UIKit
class WBVisitorView: UIView {
    //4.创建注册按钮
    lazy var registButton:UIButton = UIButton.cz_textButton("注册", fontSize: 14, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    //5.创建登录按钮
    lazy var loginButton:UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    //定义一个字典属性用于接受外部每个字控制的信息
    var visitorInfo:[String:String]? {
        didSet{
            guard let message = visitorInfo?["message"],
            let imageName = visitorInfo?["imageName"]
            else {
                return
            }
            if imageName == ""{
                startAnimation()
                return
            }
            houseImageView.image = UIImage(named: imageName)
            //隐藏转轮的图片
            circleImageView.isHidden = true
            tipLabelView.text = message
        }
    }
    override init(frame: CGRect) {
        super.init(frame:frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //1.创建圆圈图片
        fileprivate lazy var circleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //2.创建房子图片
        fileprivate lazy var houseImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //3.创建标签文字
        fileprivate lazy var tipLabelView:UILabel = UILabel.cz_label(withText: "登录后，你所关注的人的微博会显示在这里", fontSize: 14, color: UIColor.darkGray)
    //6.创建遮挡图片
        fileprivate lazy var coverImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}

extension WBVisitorView{
    func setUpUI() {
        backgroundColor = UIColor.cz_color(withRed: 237, green: 237, blue: 237)
        addSubview(circleImageView)
        addSubview(coverImageView)
        addSubview(houseImageView)
        addSubview(tipLabelView)
        addSubview(registButton)
        addSubview(loginButton)
        setPosition()
    }
    func startAnimation() {
        //CABasicAnimation就是对图层layer进行动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //旋转角度
        anim.toValue = M_PI * 2
        //旋转指定角度所需要的时间
        anim.duration = 15
        //重复次数
        anim.repeatCount = MAXFLOAT
        //动画完成后是否停留在原来的位置
        anim.isRemovedOnCompletion = false
        //将动画加入到图层中
        circleImageView.layer.add(anim, forKey: nil)
    }
    func setPosition(){
        //一定要先取消autoresizing自动调整大小
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //自动布局
        //圆圈图片
        //公式：item1.attribute1 = multiplier × item2.attribute2 + constant
        //控件1的属性值等于控件2的属性值乘以一个倍数+一个常数
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.centerX, relatedBy: .equal,toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal,toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: -60))
        //房子图片
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.centerX, relatedBy: .equal,toItem: circleImageView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal,toItem: circleImageView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
        //文字
        addConstraint(NSLayoutConstraint(item: tipLabelView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabelView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 50))
             addConstraint(NSLayoutConstraint(item: tipLabelView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 230))
        //使文字居中对齐
        tipLabelView.textAlignment = NSTextAlignment.center
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: tipLabelView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: tipLabelView, attribute: NSLayoutAttribute.left, multiplier: 1, constant: -10))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 115))
        //登录
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: tipLabelView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: registButton, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 25))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 115))
        //遮挡物
        //views:定义VFL中控件名称和实际名称的映射关系
        //metrics:定义VFL中()中常数的映射关系
        let coverView = ["coverImageView":coverImageView]
        let metrics = ["spacing":250]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[coverImageView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: coverView))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[coverImageView]-(spacing)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: coverView))
    }
}
